﻿using Microsoft.AspNetCore.Mvc.ModelBinding;

namespace WXWorkSvc.Common.TagHelpers;
/// <summary>
/// 显示单个记录的详情，常用于Delete页面的显示
/// </summary>
[HtmlTargetElement("wx-detail", TagStructure = TagStructure.WithoutEndTag)]
public class WxDetaiTagHelper : TagHelper
{
    #region Consts
    /// <summary>
    /// 0:label,1:value
    /// </summary>
    public const string ItemStringFormat = """"
            <div class="weui-form-preview__item">
                <label class="weui-form-preview__label">{0}</label>
                <em class="weui-form-preview__value">{1}</em>
            </div>

            """";
    public const string ItemStringFormatWarn = """"
            <div class="weui-form-preview__item">
            <label class="weui-form-preview__label">{0}</label>
            <em class="weui-form-preview__value"><span style="background-color:red;color:white">{1}</span></em>
            </div>

            """";

    #endregion

    private readonly IHtmlGenerator _HtmlGenerator;
    public WxDetaiTagHelper(IHtmlGenerator htmlGenerator)
    {
        _HtmlGenerator = htmlGenerator;
    }

    [HtmlAttributeNotBound]
    [ViewContext]
    public ViewContext ViewContext { get; set; } = default!;
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-for-")]
    public Dictionary<string, ModelExpression> Fors { get; set; } = new();
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-warn-")]
    public Dictionary<string, string> Warns { get; set; } = new();
    /// <summary>
    /// 删除时保存的BindProperty
    /// </summary>
    [HtmlAttributeName(DictionaryAttributePrefix = "asp-hidden-")]
    public Dictionary<string, ModelExpression> Hiddens { get; set; } = new();
    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        output.TagName = "div";
        output.TagMode = TagMode.StartTagAndEndTag;
        output.Attributes.Add("class", "weui-form-preview");

        var firstKV = Fors.First();
        TagBuilder tagBuilderHeader = new TagBuilder("div");
        tagBuilderHeader.AddCssClass("weui-form-preview__hd");//Header
        tagBuilderHeader.InnerHtml.AppendHtml(string.Format(ItemStringFormat, firstKV.Value.GetDisplayName(), firstKV.Value.Model));
        output.Content.SetHtmlContent(tagBuilderHeader);
        Fors.Remove(firstKV.Key);

        var tagBuilder=new TagBuilder("div");
        tagBuilder.AddCssClass("weui-form-preview__bd");//Body
        foreach (var kv in Fors)
        {
            var sf = Warns.ContainsKey(kv.Key) ? ItemStringFormatWarn : ItemStringFormat;
            var value = kv.Value.Model is bool bb ? (bb ? "是" : "否") : kv.Value.Model.ToString();
            tagBuilder.InnerHtml.AppendHtml(string.Format(sf, kv.Value.GetDisplayName(), value));
        }
        output.Content.AppendHtml(tagBuilder);
        if (Hiddens.Count > 0)
        {
            foreach (var kv in Hiddens)
                output.Content.AppendHtml(_HtmlGenerator.GenerateHidden(ViewContext, kv.Value.ModelExplorer, kv.Value.Name, kv.Value.Model, false, null));
        }
    }
}
